//
//  JobDetailJobSiteInstructionView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/23.
//

import UIKit
import RxCocoa
import RxSwift
import SnapKit
class JobDetailJobSiteInstructionView: View {
    private lazy var disposeBag = DisposeBag()
    let instructionTitle = ContentLabel(R.font.interRegular(size: 12),hexStr: "#292D3299")
    let instructionText = PaddingLabel()
    override func setUpView() {
        
        //*******
        instructionTitle.text = "JOB SITE INSTYUCTIONS"
        self.addSubview(instructionTitle)
        instructionTitle.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.left.equalTo(24)
            make.width.height.greaterThanOrEqualTo(0)
        }
        let moreBtn = UIButton()
        self.addSubview(moreBtn)
        moreBtn.snp.makeConstraints { make in
            make.centerY.equalTo(instructionTitle)
            make.right.equalTo(-20)
            make.width.height.equalTo(40)
        }
        //
        instructionText.padding = UIEdgeInsets(top: 20, left: 16, bottom: 20, right: 16)
        instructionText.backgroundColor = .white
        instructionText.layer.cornerRadius = Constant.BackViewCornerRadius
        instructionText.clipsToBounds = true
        instructionText.setFont(R.font.interMedium(size: 13))
        instructionText.textColor = UIColor(hexStr: "#292D32B2")
        instructionText.numberOfLines = 0
        instructionText.textAlignment = .left
        self.addSubview(instructionText)
        instructionText.snp.makeConstraints { make in
            make.top.equalTo(instructionTitle.snp.bottom).offset(9)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
            
        }
        moreBtn.tag =  1
        moreBtn.setImage(R.image.more_gray_down_icon(), for: UIControl.State.normal)
        moreBtn.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let strongSelf = self else {return}
            if moreBtn.tag == 1 {//hide instruction
                moreBtn.tag =  0
                moreBtn.setImage(R.image.more_gray_up_icon(), for: UIControl.State.normal)
                strongSelf.instructionText.snp.remakeConstraints { make in
                    make.top.left.right.height.equalTo(strongSelf.instructionTitle)
                    make.bottom.equalToSuperview()
                }
                strongSelf.instructionText.padding = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
                strongSelf.instructionText.text = ""
                strongSelf.instructionText.isHidden = true
            }else{//show instruction
                moreBtn.tag =  1
                moreBtn.setImage(R.image.more_gray_down_icon(), for: UIControl.State.normal)
                strongSelf.instructionText.snp.remakeConstraints { make in
                    make.top.equalTo(strongSelf.instructionTitle.snp.bottom).offset(9)
                    make.left.equalTo(12)
                    make.right.equalTo(-12)
                    make.height.greaterThanOrEqualTo(0)
                    make.bottom.equalToSuperview()
                    
                }
                strongSelf.instructionText.padding = UIEdgeInsets(top: 20, left: 16, bottom: 20, right: 16)
                strongSelf.instructionText.text = JobDetailModelHelper.shared.jobDetailModel.installer_job_site_instruction
                strongSelf.instructionText.isHidden = false
            }
        }).disposed(by: disposeBag)
        
        JobDetailModelHelper.shared.installer_job_site_instruction.subscribe { [weak self] (data) in
            self?.instructionText.text = data
            
        }.disposed(by: self.disposeBag)
        
    }
    
}
